package com.lcsw.background.controller;

import com.alibaba.fastjson.JSONObject;
import com.lcsw.background.ExportExcel.util.ExportExcelUtil;
import com.lcsw.background.ExportExcel.util.ExportExcelWrapper;
import com.lcsw.background.domain.AdminPlus;
import com.lcsw.background.domain.PayDown;
import com.lcsw.background.domain.Tradeserial;
import com.lcsw.background.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/down")
public class DownController {
    @Autowired
    AdminService adminService;

    @RequestMapping("/payList")
    public JSONObject downPay(@RequestBody(required = false) String json1, HttpServletRequest request, HttpServletResponse response) {
        // 准备数据
        if (json1 == null || json1 == "") {
            JSONObject object = new JSONObject();
            object.put("msg", "异常");
            return object;
        }
        JSONObject json = null;
        try {
            json = (JSONObject) JSONObject.parse(json1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String username = "";
        Object obj = request.getSession().getAttribute("username");
        if (obj == null) {
            JSONObject object = new JSONObject();
            object.put("msg", "请先点击显示详情");
            return object;
        }
        if (request.getSession().getAttribute("username") != null) {
            username = request.getSession().getAttribute("username").toString();
        }
        String merchant_no = "";
        if (StringUtils.isEmpty(json.get("merchant_no"))) {
            merchant_no = "";
        } else {
            merchant_no = json.get("merchant_no").toString();
        }
        String terminalno;
        if (StringUtils.isEmpty(json.get("terminalno"))) {
            terminalno = null;
        } else {
            terminalno = json.getInteger("terminalno").toString();
        }
        String out_trade_no = "";
        if (StringUtils.isEmpty(json.get("out_trade_no"))) {
            out_trade_no = "";
        } else {
            out_trade_no = json.get("out_trade_no").toString();
        }
        List<Tradeserial> list = adminService.queryDetail(username, merchant_no, terminalno, out_trade_no);
        List<PayDown> payList = new ArrayList<>();
        for (Tradeserial t : list) {
            PayDown payDown = new PayDown();
            payDown.setMerchantNo(t.getMerchantNo());
            payDown.setTerminalno(t.getTerminalno());
            payDown.setOutTradeNo(t.getOutTradeNo());
            payDown.setTotalFee(t.getTotalFee() / 100d);
            payDown.setEndtime(t.getEndtime());
            if ("1".equals(t.getPayStatusCode().toString())) {
                payDown.setPayStatusCode("支付成功");
            } else if ("2".equals(t.getPayStatusCode().toString())) {
                payDown.setPayStatusCode("支付失败");
            }
            if ("支付成功".equals(t.getPayStatusMsg())) {
                payDown.setPayStatusMsg("");
            } else {
                payDown.setPayStatusMsg(t.getPayStatusMsg());
            }
            payList.add(payDown);
        }
        String[] columnNames = {"商户号", "终端号", "订单号", "支付金额(元)", "支付时间", "支付状态", "失败原因"};
        String fileName = "payExcel";
        ExportExcelWrapper<PayDown> util = new ExportExcelWrapper<>();
        util.exportExcel(fileName, fileName, columnNames, payList, response, ExportExcelUtil.EXCEL_FILE_2003);
        return null;
    }


    @RequestMapping("/userList")
    public void downUser(@RequestBody(required = false) String json1, HttpServletRequest request, HttpServletResponse response) {
        // 准备数据
        List<AdminPlus> list = new ArrayList<>();
        JSONObject object = new JSONObject();
        if (json1 == null || json1 == "") {//没有参数时,返回查到的所有用户
            list = adminService.payList("");
        }
        String username = "";
        String condition = "";
        JSONObject json = null;
        try {
            json = (JSONObject) JSONObject.parse(json1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (StringUtils.isEmpty(json.get("username"))) {
            if (StringUtils.isEmpty(json.get("condition"))) {//condition=null
                list = adminService.payList(username);
            }
            condition = json.getString("condition");
            if ("0".equals(condition)) {//全查询
                list = adminService.payList(username);
            }
            if ("1".equals(condition)) {// 完成状态为已完成
                list = adminService.queryCompletePay();
            }
            if ("2".equals(condition)) {
                list = adminService.queryUndonePay();
            }
        }
        if (!StringUtils.isEmpty(json.get("username"))) {//只传username按username查
            username = json.getString("username");
            if (StringUtils.isEmpty(json.get("condition"))) {//condition=null
                list = adminService.payList(username);
            }
            condition = json.getString("condition");
            if ("0".equals(condition)) {//全查询
                list = adminService.payList(username);
            }
            if ("1".equals(condition)) {//username 完成状态为已完成
                list = adminService.queryUserAndComCondition(username);
            }
            if ("2".equals(condition)) {
                list = adminService.queryUserAndUnCondition(username);
            }
        }
        String[] columnNames = {"", "用户名", "昵称", "电话", "邮箱", " 二维码总数", "已处理", "未处理", "成功笔数", "失败数量"};
        String fileName = "userExcel";
        ExportExcelWrapper<AdminPlus> util = new ExportExcelWrapper<>();
        util.exportExcel(fileName, fileName, columnNames, list, response, ExportExcelUtil.EXCEL_FILE_2003);
    }
}
